

import java.awt.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JApplet;


/**
 * This applet simulates the behaviour of the sl package for Debain in java
 * @author Raymond Vermaas
 */
public class Jsl extends JApplet implements Runnable {

    Thread runner;
    int Xpos = 500;
    int state = 0;
    MP3 mp3 = new MP3("train.mp3");

    @Override
    public void start() {
        if(runner == null) {
            runner = new Thread(this);
            runner.start();
        }
    }

    @Override
    public void stop() {
        if(runner != null) {
            runner = null;
        }
    }

    @Override
    public void run() {
        mp3.play();
        while(1 == 1){
            repaint();
            state++;
            if(state == 4)
                state = 0;
            try {
                runner.sleep(100);
            } catch (InterruptedException ex) {
                Logger.getLogger(Jsl.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    @Override
    public void paint(Graphics gr) {
        //Set the font to the console font
        Font font = new Font("Lucida Console",Font.PLAIN,12);

        //Create a off screen buffer to minimize flickering
        Image bufferImage = createImage(this.getSize().width, this.getSize().height );
        Graphics bufferGraphics = bufferImage.getGraphics();
        bufferGraphics.setFont(font);
        int Ypos = 25;
        String[] smoke = getSmoke();
        for(String line  : smoke) {
            bufferGraphics.drawString(line, Xpos, Ypos);
            Ypos+=12;
        }
        String[] train = getTrain();
        for(String line  : train) {
            bufferGraphics.drawString(line, Xpos, Ypos);
            Ypos+=12;
        }
        String[] wheels = getWheels();
        for(String line : wheels) {
            bufferGraphics.drawString(line, Xpos, Ypos);
            Ypos+=12;
        }
        if (state == 0 || state == 3) {
            bufferGraphics.drawString("You have been trained!", 200, Ypos+100);
        }
        gr.drawImage(bufferImage, 0, 0, this);
        Xpos = Xpos-20;
        if(Xpos < -800)
            Xpos=500;
    }

    public String[] getTrain() {
        // use alt+0160 for the first white space
        String[] train = {"      ====        ________                ___________ " ,
                          "  _D _|  |_______/        \\__I_I_____===__|_________|" ,
                          "   |(_)---  |   H\\________/ |   |        =|___ ___|       _________________" ,
                          "   /     |  |   H  |  |     |   |         ||_| |_||     _|                \\_____A" ,
                          "  |      |  |   H  |__--------------------| [___] |   =|                        |" ,
                          "  | ________|___H__/__|_____/[][]~\\_______|       |   -|                        |" ,
                          "  |/ |   |-----------I_____I [][] []  D   |=======|____|________________________|_" };
        return train;
    }

    public String[] getSmoke() {
        String[] smoke = new String[6];
        switch (state) {
            case 0: smoke[0] = "                        (  ) (@@) (  )  (@)  ( )    @@     ()     @     ()    @      ()";
                    smoke[1] = "                   (@@@)";
                    smoke[2] = "               (    )";
                    smoke[3] = "            (@@@@)";
                    smoke[4] = "";
                    smoke[5] = "         (     )";break;
            case 1: smoke[0] = "                        (@@@) (  ) (@@)  ( )  (@)    ()    @@     ()     @     ()      @";
                    smoke[1] = "                   (   )";
                    smoke[2] = "               (@@@@)";
                    smoke[3] = "            (    )";
                    smoke[4] = "";
                    smoke[5] = "         (@@@@)";break;
            case 2: smoke[0] = "                        (  ) (@@) (  )  (@)  ( )    @@     ()     @     ()     @      ()";
                    smoke[1] = "                   (@@@)";
                    smoke[2] = "               (    )";
                    smoke[3] = "            (@@@@)";
                    smoke[4] = "";
                    smoke[5] = "         (     )";break;
            case 3: smoke[0] = "                        (@@@) (  ) (@@)  ( )  (@)    ()     @@     ()     @    ()      @";
                    smoke[1] = "                   (   )";
                    smoke[2] = "               (@@@@)";
                    smoke[3] = "            (    )";
                    smoke[4] = "";
                    smoke[5] = "         (@@@@)";break;
            default: smoke[0] = "                        (@@@) (  ) (@@)  ( )  (@)    ()    @@     ()     @    ()      @";
                    smoke[1] = "                   (   )";
                    smoke[2] = "               (@@@@)";
                    smoke[3] = "            (    )";
                    smoke[4] = "";
                    smoke[5] = "         (@@@@)";break;
    }
        
        return smoke;
    }

    public String[] getWheels() {
        String[] wheels = new String[3];
        switch (state) {
            case 0: wheels[0] = "__/ =| o |=-~0=====0=====0=====0\\ ____Y___________|__";
                    wheels[1] = " |/-=|___||    ||    ||    ||    |_____/~\\___/";
                    wheels[2] = "  \\_/      \\__/  \\__/  \\__/  \\__/      \\_/"; break;
            case 1: wheels[0] = "__/ =| o |=-~~\\  /~~\\  /~~\\  /~~\\ ____Y___________|__";
                    wheels[1] = " |/-=|___||0=====0=====0=====0   |_____/~\\___/";
                    wheels[2] = "  \\_/      \\__/  \\__/  \\__/  \\__/      \\_/"; break;
            case 2: wheels[0] = "__/ =| o |=-~~\\  /~~\\  /~~\\  /~~\\ ____Y___________|__";
                    wheels[1] = " |/-=|___||    ||    ||    ||    |_____/~\\___/";
                    wheels[2] = "  \\_/      \\0=====0=====0=====0_/      \\_/"; break;
            case 3: wheels[0] = "__/ =| o |=-~~\\  /~~\\  /~~\\  /~~\\ ____Y___________|__";
                    wheels[1] = " |/-=|___||   0=====0=====0=====0|_____/~\\___/";
                    wheels[2] = "  \\_/      \\__/  \\__/  \\__/  \\__/      \\_/"; break;
            default: wheels[0] = "__/ =| o |=-~~\\  /~~\\  /~~\\  /~~\\ ____Y___________|__";
                     wheels[1] = " |/-=|___||    ||    ||    ||    |_____/~\\___/";
                     wheels[2] = "  \\_/      \\__/  \\__/  \\__/  \\__/      \\_/"; break;
        }
        wheels[0] += "|__________________________|_";
        wheels[1] += "          |_D__D__D_|  |_D__D__D_|";
        wheels[2] += "               \\_/   \\_/    \\_/   \\_/";
        return wheels;
    }

}
